package com.example.sunsg.okhttptest.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by sunsg on 16/10/25.
 */
public class ScreenShotsUtil {
    public static void normalScreenShots(View v) {
        if(v == null){
            return;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.CHINA);

        String fname = StorageUtils.mkdir() + "/" + sdf.format(new Date()) + ".png";

        View view = v.getRootView();

        view.setDrawingCacheEnabled(true);

        view.buildDrawingCache();

        Bitmap bitmap = view.getDrawingCache();

        if (bitmap != null) {

            Log.i("test", "bitmap got !");

            try {

                FileOutputStream out = new FileOutputStream(fname);

                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);


                Log.i("test", "file " + fname + " output done.");

            } catch (Exception
                    e) {

                e.printStackTrace();

            }

        } else {
            Log.i("test", "bitmap is null");

        }
    }

    public static void screenShots(Activity context){
        View view  = context.getWindow().getDecorView();
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
//        保存到文件

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.CHINA);
            String fileName = StorageUtils.mkdir() +"/"+sdf.format(new Date())+"webview_capture4.jpg";
            FileOutputStream fos = new FileOutputStream(fileName);
            //压缩bitmap到输出流中
            bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
            fos.close();
            Toast.makeText(context, "截屏成功", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Log.e("test", e.getMessage());
        }finally {
            if(bitmap!=null) {
                bitmap.recycle();
            }

        }
    }

    public static void screenShots(WebView webView) {
        //获取webview缩放率
        float scale = webView.getScale();
        //得到缩放后webview内容的高度
        int webViewHeight = (int) (webView.getContentHeight() * scale);
        Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), webViewHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        //绘制
        webView.draw(canvas);

        if (bitmap != null) {

            Log.i("test", "bitmap got !");

            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.CHINA);
                String fileName = StorageUtils.mkdir() +"/"+sdf.format(new Date())+"webview_capture4.jpg";
                FileOutputStream out = new FileOutputStream(fileName);

                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);


                Log.i("test", "file " + fileName + " output done.");

            } catch (Exception
                    e) {

                e.printStackTrace();

            }

        } else {
            Log.i("test", "bitmap is null");

        }
    }


}
